package com.cn.aam.checaiduo.ui.more;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.base.BaseResponseB;
import com.cn.aam.checaiduo.center.CustomDialogCenter;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.AuthUtil;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;


@Route(path = UIRouter.MORE.CHANGE_MOBILE_STEP2, name = "修改手机号码第2步")
public class ChangeMobileStep02Activity extends BaseActivity {
    public final static String DATA_OLD_MOBILE = "data_old_mobile";

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;


    private CountDownTimer cd;
    private boolean getCodeIng = false;//正在获取验证码中

    private String oldMobile;
    private String mobile;
    private String code;


    @Override
    protected int layoutId() {
        return R.layout.activity_change_mobile_step02;
    }

    @Override
    protected void onGenerate() {
        oldMobile = getIntent().getStringExtra(DATA_OLD_MOBILE);
    }

    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("")
                .setTitleColor(R.color.black_normal);
    }

    /**
     * 获取验证码
     */
    @OnClick(R.id.btn_code)
    public void onClickGetCode() {
        if (!CVBGetCode()) {
            return;
        }
        RetrofitHome.getInstance()
                .getInternalApi()
                .getSMSCode(mobile)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS)
                            startCountTime();
                    }
                });
    }

    /**
     * 提交
     */
    @OnClick(R.id.btn_submit)
    public void onClickSubmit() {
        if (!CVBSubmit()) {
            return;
        }
        RetrofitHome.getInstance()
                .getInternalApi()
                .verifyCodeForChangeMobile(AppData.getToken(), code, oldMobile, mobile)
                .compose(RxSchedulers.<BaseResponseB>toMain())
                .subscribe(new OnRequestListener<BaseResponseB>(this) {
                    @Override
                    public void onNext(BaseResponseB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS) {
                            CustomDialogCenter.getInstance()
                                    .showCMSDialog(mContext, new CustomDialogCenter.CallBack() {
                                        @Override
                                        public void onCall() {
                                            AppData.closeActivityExceptMain();
                                        }
                                    });
                        }
                    }
                });

    }

    public void getAllValue() {
        mobile = MyEditTextUtils.getContent(etMobile);
        code = MyEditTextUtils.getContent(etCode);
    }

    //check value before
    public boolean CVBGetCode() {
        getAllValue();
        if (TextUtils.isEmpty(mobile)) {
            WMToast.normal("请输入新手机号码");
            return false;
        }
        if (!AuthUtil.isMobileNo(mobile)) {
            WMToast.normal("请输入正确的手机号码");
            return false;
        }
        return true;
    }

    //check value before
    public boolean CVBSubmit() {
        getAllValue();
        if (TextUtils.isEmpty(mobile)) {
            WMToast.normal("请输入新手机号码");
            return false;
        }
        if (!getCodeIng) {
            WMToast.normal("请获取验证码");
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            WMToast.normal("请输入验证码");
            return false;
        }
        return true;
    }

    /**
     * 开始倒计时
     */
    private void startCountTime() {
        WMToast.normal("验证码已发送");
        getCodeIng = true;
        btnCode.setEnabled(false);
        //倒计时60s
        cd = new CountDownTimer(Const.MSG_COUNT_DOWN * 1000, 1000) {
            @Override
            public void onTick(long l) {
                if (btnCode == null) return;
                btnCode.setText("重新发送(" + l / 1000 + "s)");
            }

            @Override
            public void onFinish() {
                endCountTime();
            }
        };
        cd.start();
    }

    /**
     * 结束倒计时
     */
    private void endCountTime() {
        if (cd != null) {
            cd.cancel();
            cd = null;
        }
        getCodeIng = false;
        if (btnCode == null) return;
        btnCode.setText("获取验证码");
        btnCode.setEnabled(true);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        endCountTime();
    }
}
